# -*- coding:UTF-8 -*-

from django.db import models
from django.conf import settings


# 订单模型类
class Work(models.Model):
    # 合同编号
    agreement_code = models.CharField(max_length=20, default='000000', unique=False, verbose_name='合同编号')
    # 订单编号(条形码)
    passcode = models.CharField(max_length=20, default='DL00000000000000', unique=True, verbose_name='订单编号')
    # 品牌名称
    brand_name = models.CharField(max_length=10, default='无', unique=False, verbose_name='品牌名称')
    # 所属人
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, db_constraint=False, null=True,
                              default=None, related_name='userOfWork')
    # 商家名称
    name = models.CharField(max_length=20, verbose_name='商家名称')
    # 订单下商品数量
    total = models.IntegerField(max_length=5, null=False, default=1, verbose_name='订单下商品数量')
    # 订单类型 1为批量 2为自主
    type = models.IntegerField(verbose_name='订单类型')
    # 完成时间
    finished_date = models.DateField(default=None, null=True, verbose_name='订单完成时间')
    # 联系人
    contact_name = models.CharField(max_length=6, default='无', unique=False, verbose_name='联系人')
    # 联系电话
    contact_phone = models.CharField(max_length=15, default='0000000', unique=False, verbose_name='联系电话')
    # 公司地址
    area = models.CharField(max_length=30, default='无', unique=False, verbose_name='地址')
    # 订单状态 默认为1：待发布,2：进行中 3：完成
    state = models.IntegerField(verbose_name='订单完成状态', default=1)
    # 备注
    remark = models.TextField(verbose_name='备注', null=True, default=None)
    # 模板
    template = models.CharField(max_length=20, verbose_name='模板', null=True, default=None)
    # 录入人
    create_user = models.CharField(max_length=10, verbose_name='录入人', null=True, default=None)
    # 录入时间
    create_date = models.DateField(verbose_name='录入时间', default=None)

    class Meta:
        db_table = 'dl_work'
        verbose_name = '订单'
        verbose_name_plural = verbose_name


# 商品模型类
class Goods(models.Model):
    # 品牌名字
    name = models.CharField(max_length=20, verbose_name="品牌名字", default='无')
    # 商品编号（条形码）
    code = models.CharField(max_length=20, verbose_name='商品编号', default='0000000000N000-000', unique=True, null=False)
    # 商品对应的订单 一个订单里有多本书
    work = models.ForeignKey(Work, on_delete=models.CASCADE, db_constraint=False, null=True, default=1, related_name='bookOfWork')
    # 拍摄完成状态
    shooting_status = models.BooleanField(default=False, verbose_name='拍摄完成状态')
    # 商品编号是否激活
    is_active = models.BooleanField(verbose_name='是否激活', default=True)
    # 创建时间
    create_date = models.DateField(auto_now_add=True, verbose_name='创建时间')
    # 完成状态 从创建订单开始到最终审核通过即为完成
    finished = models.BooleanField(verbose_name='完成状态', default=False)
    # 拍摄完成时间
    shooting_finished_date = models.DateField(null=True, default=None, verbose_name='拍摄完成时间')
    # 是否归还图书
    return_back = models.BooleanField(verbose_name="是否归还物品", default=False)

    class Meta:
        db_table = 'dl_goods'
        verbose_name = '书籍'
        verbose_name_plural = verbose_name


# 任务阶段模型类
class StageName(models.Model):
    name = models.CharField(max_length=10, verbose_name='阶段名称', default=None)

    class Meta:
        db_table = 'dl_stage_name'
        verbose_name = '任务阶段名称'
        verbose_name_plural = verbose_name


# 任务模型类
class Task(models.Model):
    # 录入图书的编号
    code = models.ForeignKey(Goods, on_delete=models.CASCADE, db_constraint=False, null=True, default=1, related_name='taskOfBook')
    # 当前阶段的名称 1为录入 2为前期 3为分片 4为后期 5最终审核 6完成
    stage = models.ForeignKey(StageName, on_delete=models.CASCADE, db_constraint=False, null=True, default=1, related_name='taskOfStage')
    # 当前任务阶段类型 1为平面 2为视频
    stage_type = models.IntegerField(verbose_name='当前阶段类型', default=None)
    # 当前阶段的状态 1为等待分配 2为进行中 3为已完成 4待审核
    state = models.IntegerField(verbose_name='当前任务阶段状态')
    # 下一阶段名称
    next_stage = models.IntegerField(verbose_name='下一阶段名称')
    # 创建任务的时间
    create_date = models.DateField(auto_now_add=True, verbose_name='创建时间')
    # 任务创建人
    create_user = models.CharField(max_length=10, verbose_name='创建人', default="Null")
    # 任务到期时间
    diedline = models.DateField(verbose_name='任务截止时间')

    class Meta:
        db_table = 'dl_task'
        verbose_name = '任务'
        verbose_name_plural = verbose_name


# 历史记录
class History(models.Model):
    book = models.ForeignKey(Goods, on_delete=models.CASCADE, db_constraint=False,
                             null=True, default=1, related_name='historyOfBook')
    task = models.ForeignKey(Task, on_delete=models.CASCADE, default=1, related_name='historyOfTask')
    stage = models.ForeignKey(StageName, on_delete=models.CASCADE, db_constraint=False,
                              null=True, default=1, related_name='historyOfStage')
    start_time = models.DateField(verbose_name='开始时间')
    finish_time = models.DateField(verbose_name='完成时间', null=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='historyOfUsers')

    class Meta:
        db_table = 'dl_history'
        verbose_name = '历史记录'
        verbose_name_plural = verbose_name


# 问题商品
class Problem(models.Model):
    code = models.ForeignKey(Goods, on_delete=models.CASCADE, db_constraint=False,
                             null=False, default='000000', related_name='problemOfBook')
    quantity = models.IntegerField(verbose_name='数量')
    remark = models.TextField(null=True, verbose_name='备注')
    receiver = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='problemOfUsers')
    solution = models.TextField(verbose_name='解决方案')
    picture = models.CharField(max_length=30, null=True, verbose_name='上传图片地址')

    class Meta:
        db_table = 'dl_problem'
        verbose_name = '问题商品'
        verbose_name_plural = verbose_name


# 物流信息
class Logistics(models.Model):
    passcode = models.ForeignKey(Work, on_delete=models.CASCADE, db_constraint=False,
                             null=False, default='000000', related_name='logisticsOfWork')
    # 收退货 1位接货 2位退货
    receive_or_return = models.IntegerField(default=1, verbose_name="接货还是退货")
    user = models.CharField(max_length=10, null=False, default="User", verbose_name='人员')
    contact = models.CharField(max_length=20, null=False, default="PhoneNum", verbose_name='联系方式')
    date = models.DateField(auto_now_add=True, verbose_name='创建时间')
    # 1为快递  2为自主
    style = models.IntegerField(max_length=1, null=False, default=1, verbose_name='方式')
    tracking_number = models.CharField(max_length=30, null=True, verbose_name='快递单号')
    tracking_company = models.CharField(max_length=10, null=True, verbose_name='快递公司名称')
    postman = models.CharField(max_length=5, null=True, verbose_name='送货人')
    post_style = models.CharField(max_length=10, null=True, verbose_name='送货方式')

    class Meta:
        db_table = 'dl_logistics'
        verbose_name = '收退货记录'
        verbose_name_plural = verbose_name


